﻿using ST.Torch.WebApi.Application.System.Services;
using ST.Torch.WebApi.Application.Utils.Dtos;
using ST.Torch.WebApi.Application.Utils.Services;
using ST.Torch.WebApi.Core.Manager;
using Newtonsoft.Json;
using Furion.HttpRemote;

namespace ST.Torch.WebApi.Application.Utils
{
    /// <summary>
    /// 微信接口
    /// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
    /// </summary>
    public class WeChatAppService : IDynamicApiController
    {
        public string appid = "wxe406fe2d200cd668";
        public string appsecret = "74e01414082b904019c890baae84a9b3";

        public WeChatAppService()
        {
            
        }

        /// <summary>
        /// 小程序获取openId
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<string> GetOpenid(string code)
        {
            var _httpRemoteService = App.GetService<IHttpRemoteService>();
            string url = $"https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={appsecret}&js_code={code}&grant_type=authorization_code";
            var res = await _httpRemoteService.GetAsync(url);
            string content = await res.Content.ReadAsStringAsync();
            var tempData = JsonConvert.DeserializeObject<jscode2sessionModel>(content);

            return tempData.openid;

        }


    }
}
